package yamlApply

import (
	"context"
	"gitee.com/Frank098/devops-app-mgt/model"
	"gitee.com/Frank098/devops-app-mgt/service"
	appsv1 "k8s.io/api/apps/v1"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// CreateDeploy 创建自定义控制器
func CreateDeploy(name, ns string, label map[string]string, spec appsv1.DeploymentSpec) (*appsv1.Deployment, error) {
	rs := appsv1.Deployment{
		TypeMeta: metav1.TypeMeta{APIVersion: "apps/v1", Kind: "Deployment"},
		ObjectMeta: metav1.ObjectMeta{
			Name:      name,
			Namespace: ns,
			Labels:    label,
		},
		Spec: spec,
	}
	create, err := service.K8s.ClientSet.AppsV1().Deployments(ns).Create(context.TODO(), &rs, metav1.CreateOptions{})
	return create, err
}

// UpdateDeploy 更新deploy
func UpdateDeploy(deploy *appsv1.Deployment) (*model.Response, error) {
	_, err := service.K8s.ClientSet.AppsV1().Deployments(deploy.Namespace).Update(context.TODO(), deploy, metav1.UpdateOptions{})
	if err != nil {
		return nil, err
	}
	return &model.OK, nil
}
